﻿var speed = 300;
var user = NaN;
$(document).ready(function() {
    $("#dChatRoom").hide();
    $("#dLogin").show(speed);
    //登录按钮事件
    $("#btnLogin").click(function(event) {
        event.stopPropagation();
        if ($("#txtNickname").val()) {
            user = $("#txtNickname").val();
            $('#dSource').text(user);
            $("#dLogin").hide(speed);
            $("#dChatRoom").show(speed);
            //开启消息连接
            doConnect();
            return true;
        }
        else
            alert("请输入昵称!");
    });
    //昵称框回车事件
    $('#txtNickname').keydown(function(e) {
        if (e.keyCode == 13) {
            $('#btnLogin').click();
        }
    });
    //发送消息事件
    $('#btnSend').click(function(event) {
        if ($('#txtMessage').val()) {
            sendMessage($('#txtMessage').val());
            $('#txtMessage').val('');
        }
        else
            alert('请输入要发送的内容!');
    });
    //消息框回车事件
    $('#txtMessage').keydown(function(e) {
        if (e.keyCode == 13) {
            $('#btnSend').click();
        }
    });

});
//通过ajax发送消息
function sendMessage(msg) {
    $.post("postmsg.aspx?r=" + Math.random(), { type: 'send', user: user, message: msg, time: new Date().toTimeString() },
        function(data, textStatus) {
    //发送成功则输出本次发送的内容，未进行错误判断
            var txt = '<b1 color="blue">' + user + '</b1>' + ":" + msg + "<b3>[" + new Date().toLocaleTimeString() + "]</b3>";
            $('#dChatPanel').append('<m2><div class="triangle-right right">' + txt + '</div></m2>');
        }, "text");
        $("#dChatPanel").animate({ scrollTop: $("#dChatPanel").outerHeight(true) }, speed); //自动滚动
    }
//reverse ajax，接收消息的变长连接处理
function doConnect() {
    $.ajax({
        type: "POST",
        url: "response.aspx?r=" + Math.random(),
        dataType: "text",
        timeout: 30000,
        data: { type: 'loop', user: user, time: new Date().toTimeString() },
        success: function(data, textStatus) {
            recMessage(data);
            setTimeout(function() { doConnect(); }, 200); //重新连接,其实模拟的就是变长轮询
        },
        complete: function(XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.readyState == "4") {
                //alert(XMLHttpRequest.responseText);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            if (textStatus == "timeout")
                setTimeout(function() { doConnect(); }, 200); //timeout后重新连接
            else {
                //alert("error:" + textStatus);
            }
        }
    });
}
//接收到消息的ui处理
function recMessage(msg) {
    if (msg.length > 0) {
        var data = NaN;
        eval('data='+msg);
        var txt = '<b2>' + data.source + '</b2>' + ":" + data.msg + "<b3>[" + data.time + "]</b3>";
        $('#dChatPanel').append('<m1><div class="triangle-isosceles">' + txt + '</div></m1>');
        $("#dChatPanel").animate({ scrollTop: $("#dChatPanel").outerHeight(true) }, speed); //自动滚动
    }
}